package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Controller
 */
@MultipartConfig
@WebServlet("/Controller")
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		req.setCharacterEncoding("UTF-8");

		String button = req.getParameter("button");
		if (button != null) {
			button = button.toLowerCase();
			if (button.equals("registrar")) {
				req.getRequestDispatcher("Register").forward(req, resp);
			} else if (button.equals("login")) {
				// FIXME Dando stackoverflow quando logado
				req.getRequestDispatcher("Login").forward(req, resp);
			} else if (button.equals("submeter")) {
				req.getRequestDispatcher("Submit").forward(req, resp);
			} else if (button.equals("pesquisar")) {
				req.getRequestDispatcher("Search").forward(req, resp);
			} else if (button.equals("download")) {
				req.getRequestDispatcher("Download").forward(req, resp);
			} else if (button.equals("logout")) {
				req.getRequestDispatcher("Logout").forward(req, resp);
			}
		}
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		process(request, response);
	}

}
